/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kallsonys.session;

import com.kallsonys.dao.OrderDAO;
import com.kallsonys.esb.SelectManufacturerImplementation;
import com.kallsonys.esb.SelectManufacturerResponse;
import com.kallsonys.object.OrderOms;
import com.kallsonys.object.ws.ListElement;
import com.kallsonys.service.BilledMont;
import com.kallsonys.service.ElementListPage;
import com.kallsonys.service.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.sql.DataSource;

/**
 *
 * @author alladiab
 */
@Stateless
public class OrderOmsFacade extends AbstractFacade<OrderOms> {
    @PersistenceContext(unitName = "Kallsonys-WarPU")
    private EntityManager em;
    
    private com.kallsonys.dao.OrderDAO dao;
    @Resource(mappedName = "jdbc/OMS", type = DataSource.class)
    DataSource dsOMS;

    @PostConstruct
    void postConstruct() {
        dao = new OrderDAO(dsOMS);
    } 

    public EntityManager getEntityManager() {
        return em;
    }

    public OrderOmsFacade() {
        super(OrderOms.class);
    }
    
    
    public Order getById(long idOrder) throws Exception
    {
        Order order = dao.getById(idOrder);
        
        return order;
    }
    
    public ElementListPage<Order> getOpenOrders(int page) throws Exception
    {
        ElementListPage<Order> order = dao.getOpenOrders(page);
        
        return order;
    }
    
    public ElementListPage<Order> getClosedOrders(int page, Date startDate, Date endDate) throws Exception
    {
        ElementListPage<Order> order = dao.getClosedOrders(page, startDate,endDate);
        
        return order;
    }
    
    public List<Order> getByIdProduct(long idProduct) throws Exception
    {
        List<Order> orders = dao.getOrdersByIdProduct(idProduct);
        
        return orders;
    }

    public BilledMont getBilledMonth(int month, int year) throws Exception
    {
        BilledMont billedMont = dao.getBilledMonth(year,month);
        
        return billedMont;
    }
    
     public OrderOms getByOrderId(long orderID) {
        OrderOms c = null;

        Query queryfindByProductidproduct = em.createNamedQuery("OrderOms.findByOrderid");
        queryfindByProductidproduct.setParameter("orderid", orderID);
        c = (OrderOms) queryfindByProductidproduct.getSingleResult();
        return c;
    }
    
    public List<ListElement> getClientCategorys()
    {
        List<ListElement> lstItems = new ArrayList<ListElement>();
        
        lstItems.add(new ListElement("1", "Aprobada"));
        lstItems.add(new ListElement("2", "En Analisis"));
        lstItems.add(new ListElement("3", "En Despacho"));
        lstItems.add(new ListElement("4", "Entregada"));/*----------------------*/
        lstItems.add(new ListElement("5", "Rechazada"));
        lstItems.add(new ListElement("6", "En Proveedor Mensajeria")); /*----------------------*/
        lstItems.add(new ListElement("7", "En Transito")); /*----------------------*/
        
        return lstItems;
    }
    
    
    public SelectManufacturerResponse processOrder(long orderId)
    {
        com.kallsonys.esb.SelectManufacturerImplementation imp = new SelectManufacturerImplementation();
        
        SelectManufacturerResponse res = imp.ProcessOrder(orderId);
        
        return res;
    }
    
    
}
